package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.DeviceDictConstants;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.DyzsblqIdReq;
import com.bci.pwtz.common.remote.req.DyzsblqListReq;
import com.bci.pwtz.common.remote.req.DyzsblqReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Dyzsblq;
import com.bci.pwtz.service.DictionaryAuditService;
import com.bci.pwtz.service.DyzsblqService;
@Controller
@RequestMapping("/api/dyzsblq")
public class DyzsblqAppController extends AbstractController
{
    @Autowired
    DyzsblqService service;//注解注入服务
    @Autowired
    private DictionaryAuditService dictionaryAuditService;
    /**
     * app端请求获取设备列表方法
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:24:42<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value="/list",method=RequestMethod.POST)
    @ResponseBody
    public ListResponse<Dyzsblq> getDyzsblqList(HttpServletRequest request){
        DyzsblqListReq req=null;
        PaginationList<Dyzsblq> pagination=null;
        ListResponse<Dyzsblq> resp=new ListResponse<Dyzsblq>();
        try{
            req=getJsonToObj(request,DyzsblqListReq.class);
            if(req==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getCompanyId()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(req.getSecretKey()==null){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥为空");
                return resp;
            }
            if(req.getTimestamp()==null){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(StringUtils.isBlank(req.getToken())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(!validateToken(req.getToken(), req.getUserId())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            String beginTime=req.getBeginTime();
            Long companyId=req.getCompanyId();
            String devicename=req.getDeviceName();
            pagination=service.selectDyzsblqs(devicename,companyId,PwtzConstants.PORTAL_TYPE_APP,req.getMedLineCodes(),null,beginTime, null, req.getPageSize(),req.getPage());
            if(pagination !=null){
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("查询低压柱上避雷器列表成功");
                resp.setObjs(pagination);
                resp.setTimestamp(req.getTimestamp());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("查询低压柱上避雷器列表调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }catch(PwtzDatabaseException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }catch(Exception e){
            resp.setReturnCode(ReturnCode.OTHER_ERROR);
            resp.setReturnMsg("系统错误");
            req.setTimestamp(resp.getTimestamp());
        }
        return resp;
    }
    /**
     * app端请求添加设备方法
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:26:43<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dyzsblq> addDyzsblq(HttpServletRequest request){
        DyzsblqReq req=null;
        ObjResponse<Dyzsblq> resp=new ObjResponse<Dyzsblq>();
        try{
            req=getJsonToObj(request,DyzsblqReq.class);
            if(req==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getObj()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(req.getSecretKey()==null){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥为空");
                return resp;
            }
            if(req.getTimestamp()==null){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(StringUtils.isBlank(req.getToken())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(!validateToken(req.getToken(), req.getUserId())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            Dyzsblq rtObj=null;
            int result=0;
            if(service.load(req.getObj().getDeviceid())!=null){
                result=service.update(req.getObj());
                rtObj=req.getObj();
            }else{
                rtObj = service.add(req.getObj());
            }
            if(rtObj !=null||result>0){
                dictionaryAuditService.add(DeviceDictConstants.DYZSBLQ_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
                dictionaryAuditService.add(DeviceDictConstants.DYZSBLQ_MODEL_DICT_ID, req.getObj().getModel());
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("添加低压柱上避雷器成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setObj(rtObj);
                resp.setTimestamp(req.getTimestamp());
                setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压柱上避雷器,AbstractController.add,"添加低压柱上避雷器["+rtObj.getDevicename()+"]成功",req.getUserId());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("添加低压柱上避雷器调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }catch(PwtzDatabaseException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }catch(Exception e){
            resp.setReturnCode(ReturnCode.OTHER_ERROR);
            resp.setReturnMsg("系统错误");
            req.setTimestamp(resp.getTimestamp());
        }
        return resp;
    }
    /**
     * app端请求删除设备id对应的设备
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:28:26<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dyzsblq> delete(HttpServletRequest request){
        DyzsblqIdReq req=null;
        ObjResponse<Dyzsblq> resp=new ObjResponse<Dyzsblq>();
        try{
            req=getJsonToObj(request,DyzsblqIdReq.class);
            if(req==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getDeviceId()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(req.getSecretKey()==null){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥为空");
                return resp;
            }
            if(req.getTimestamp()==null){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(StringUtils.isBlank(req.getToken())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(!validateToken(req.getToken(), req.getUserId())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            String deviceid=req.getDeviceId();
            int result=service.delete(deviceid);
            if(result>0){
                Dyzsblq rtObj=service.load(deviceid);
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("删除低压柱上避雷器成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setTimestamp(req.getTimestamp());
                resp.setObj(rtObj);
                setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压柱上避雷器,AbstractController.delete,"删除低压柱上避雷器["+rtObj.getDevicename()+"]成功",req.getUserId());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("删除低压柱上避雷器调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }catch(CanNotDeleteException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("存在依赖关系，不能删除");
            resp.setTimestamp(req.getTimestamp());
        }catch(PwtzDatabaseException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }catch(Exception e){
            resp.setReturnCode(ReturnCode.OTHER_ERROR);
            resp.setReturnMsg("系统错误");
            req.setTimestamp(resp.getTimestamp());
        }
        return resp;
    }
    /**
     * app端请求修改对应的设备
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:29:22<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value="/update",method=RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dyzsblq> update(HttpServletRequest request){
        DyzsblqReq req=null;
        ObjResponse<Dyzsblq> resp=new ObjResponse<Dyzsblq>();
        try{
            req=getJsonToObj(request,DyzsblqReq.class);
            if(req==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getObj()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(req.getSecretKey()==null){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥为空");
                return resp;
            }
            if(req.getTimestamp()==null){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(StringUtils.isBlank(req.getToken())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(!validateToken(req.getToken(), req.getUserId())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            int result=service.update(req.getObj());
            if(result>0){
                Dyzsblq rtObj=service.load(req.getObj().getDeviceid());
                dictionaryAuditService.add(DeviceDictConstants.DYZSBLQ_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
                dictionaryAuditService.add(DeviceDictConstants.DYZSBLQ_MODEL_DICT_ID, req.getObj().getModel());
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("修改低压柱上避雷器成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setObj(rtObj);
                resp.setTimestamp(req.getTimestamp());
                setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压柱上避雷器,AbstractController.update,"低压柱上避雷器修改["+rtObj.getDevicename()+"]成功",req.getUserId());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("修改低压柱上避雷器调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }catch(PwtzDatabaseException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }catch(Exception e){
            resp.setReturnCode(ReturnCode.OTHER_ERROR);
            resp.setReturnMsg("系统错误");
            req.setTimestamp(resp.getTimestamp());
        }
        return resp;
    }
    /**
     * app端请求查询设备id对应的设备信息
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:30:24<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value="/load",method=RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dyzsblq> getObj(HttpServletRequest request){
        DyzsblqIdReq req=null;
        ObjResponse<Dyzsblq> resp=new ObjResponse<Dyzsblq>();
        try{
            req=getJsonToObj(request,DyzsblqIdReq.class);
            if(req==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getDeviceId()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(req.getSecretKey()==null){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥为空");
                return resp;
            }
            if(req.getTimestamp()==null){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(StringUtils.isBlank(req.getToken())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(!validateToken(req.getToken(), req.getUserId())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            Dyzsblq rtObj=service.load(req.getDeviceId());
            if(rtObj !=null){
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("查询低压柱上避雷器成功");
                resp.setObj(rtObj);
                resp.setTimestamp(req.getTimestamp());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("查询低压柱上避雷器调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }catch(PwtzDatabaseException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }catch(Exception e){
            resp.setReturnCode(ReturnCode.OTHER_ERROR);
            resp.setReturnMsg("系统错误");
            req.setTimestamp(resp.getTimestamp());
        }
        return resp;
    }
}
